package org.i1510.common.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.i1510.common.bean.PageBean;
import org.i1510.common.dao.ISysCompanyDao;
import org.i1510.common.po.SysCompany;
import org.i1510.common.service.ISysCompanyService;
import org.i1510.common.utils.BeanUtils;
import org.i1510.common.utils.DateUtils;
import org.i1510.common.utils.IDGenerator;
import org.i1510.common.utils.UserUtils;
import org.i1510.common.utils.web.ResultMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("iSysCompanyService")

public class SysCompanyServiceImpl extends BaseServiceImpl implements ISysCompanyService{

    @Autowired

    private ISysCompanyDao iSysCompanyDao;    

    
    public ResultMapper save(SysCompany basePo,HttpServletRequest request) {

        ResultMapper mapper = ResultMapper.generateFailure();

        SysCompany saved = null;

        if(null !=basePo && basePo.getDocId() != null  && !basePo.getDocId().equals("")){//修改

            SysCompany finder = iSysCompanyDao.findById(basePo.getDocId());   

            if(null != finder){

                finder.setDocLastTime(DateUtils.getNow());

if(UserUtils.getSessionUserBean(request) != null){
finder.setDocLastor(UserUtils.getSessionUserBean(request).getSysUser().getDocId());
}     
                BeanUtils.copy(finder, basePo);

                saved = iSysCompanyDao.save(finder); 

            }else{

                mapper = ResultMapper.generateFailure();

                return mapper;

            }

            
        }else{//新增

            basePo.setDocId(IDGenerator.getGenerateID());

            basePo.setDocCreateTime(DateUtils.getNow());

if(UserUtils.getSessionUserBean(request) != null){
basePo.setDocCreator(UserUtils.getSessionUserBean(request).getSysUser().getDocId());
} 
            saved = iSysCompanyDao.save(basePo);

        }

        if (saved == null) {

            mapper = ResultMapper.generateFailure();

            return mapper;

        }else{

            mapper = ResultMapper.generateSuccess();

            mapper.appendResult("docId", saved.getDocId());

            return mapper;            

        }

    }

    public ResultMapper delete(String docId) {

        ResultMapper mapper = ResultMapper.generateFailure();

        iSysCompanyDao.delete(docId);

        mapper = ResultMapper.generateSuccess();

        return mapper;

    }

    
    public SysCompany findById(String docId){

        return  iSysCompanyDao.findById(docId);

    }

    
    public List<SysCompany> findByIds(List<String> docIds){

        return (List<SysCompany>) iSysCompanyDao.findAll(docIds);

    }

    public List getPage(SysCompany sysCompany,PageBean pageBean,Map conditions) {

        Map map = new HashMap();

        //Bean转成Map

        BeanUtils.ObjectCopyMap(sysCompany, map);

        map.put("intPageIndex", pageBean.getIntPageIndex());

        map.put("intPageSize", pageBean.getIntPageSize());

if(null != conditions){

    map.putAll(conditions);

}

 List list = this.queryForListBySql("sysCompanySql.getPage", map);

        return list;

    }

    public int getPageCount(SysCompany sysCompany,Map conditions) {

        Map map = new HashMap();

        //Bean转成Map

        BeanUtils.ObjectCopyMap(sysCompany, map);

        

if(null != conditions){

    map.putAll(conditions);

}



        List list = this.queryForListBySql("sysCompanySql.getPageCount", map);        

        return list.size();

    }

}

